package org.fuys.coder.application.admin;

import org.fuys.coder.common.constants.ResultMessageConstants;
import org.fuys.coder.common.entity.Result;
import org.fuys.coder.common.exception.BusinessException;
import org.fuys.coder.common.holder.UserHolder;
import org.fuys.coder.domain.auth.model.annotation.PermissionCertify;
import org.fuys.coder.domain.auth.model.vo.UserVo;
import org.fuys.coder.domain.auth.model.vo.permission.PermissionTypeVO;
import org.fuys.coder.domain.user.model.req.repo.UserInfoModifyReq;
import org.fuys.coder.domain.user.model.req.repo.UserInfoSelectReq;
import org.fuys.coder.domain.user.service.repo.IUserService;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.application.admin
 * @className: UserAdminController
 * @author: WangJing
 * @description: 用户管理控制器
 * @date: 2024/8/7 18:14
 * @version: 1.0
 */
@RestController
@RequestMapping("/fuyscoder/admin/user")
public class UserAdminController {

    @Resource
    private IUserService userService;

    @PermissionCertify(permission = PermissionTypeVO.USER_SELECT)
    @PostMapping("/select")
    public Result selectUser(@RequestBody UserInfoSelectReq selectReq){
        try {
            List<UserVo> userVoList=userService.getUserInfoByCondition(selectReq);
            return Result.ok().data(userVoList);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PermissionCertify(permission = PermissionTypeVO.USER_MODIFY)
    @PostMapping("/modify")
    public Result modifyUser(@RequestBody UserInfoModifyReq modifyReq){
        try {
        final Integer operatorId = UserHolder.get();
        if(ObjectUtils.isEmpty(operatorId)){
            return Result.error(ResultMessageConstants.ILLEGAL_OPERATION+":此操作需要知道操作者");
        }
        modifyReq.setOperatorId(operatorId);
            boolean flag=userService.modifyUser(modifyReq);
            if (flag) {
                return Result.ok();
            }else{
                return Result.error(ResultMessageConstants.ILLEGAL_OPERATION+":请检查数据库中对应用户数据");
            }
        } catch (BusinessException e) {
            return Result.error(e.getMsg());
        }
    }
}
